package net.hands.common.web;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**  
 * 说明:
 *<br>
 *<p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @title Browser.java  
 * @package net.simpleframework.common.web  
 * @author shihb(shihaibin.sea@gmail.com, 13466609192)
 *      <p>http://code.google.com/p/simpleframework/</p>
 *      <p>http://www.simpleframework.net</p>
 * @update 2012-11-5 上午12:57:51    
 */

public class Browser  implements Serializable{
	/**
	 * 
	* 说明：获得浏览器信息
	* @param httpRequest
	* @return Browser
	* @update 2012-11-5 下午3:05:36
	 */
	public static Browser get(final HttpServletRequest httpRequest) {
		final HttpSession httpSession = httpRequest.getSession();
		Browser browser = (Browser) httpSession.getAttribute("@browser");
		if (browser == null) {
			httpSession.setAttribute("@browser",
					browser = new Browser(httpRequest.getHeader("User-Agent")));
		}
		return browser;
	}

	private boolean trident = false, webKit = false, gecko = false, presto = false;

	/* 主版本 */
	private int version = 0;
   /**
    * 
   * Constructor Method  
   * @update 2012-11-5 下午3:06:29  
   * @param userAgent 客户端信息
    */
	public Browser(final String userAgent) {
		int p;
		trident = (p = userAgent.indexOf("MSIE")) > -1;
		if (trident) {
			final int p2 = userAgent.indexOf('.', p);
			try {
				version = Integer.parseInt(userAgent.substring(p + 4, p2).trim());
			} catch (final NumberFormatException e) {
			}
		} else {
			webKit = userAgent.contains("AppleWebKit/");
			if (!webKit) {
				gecko = userAgent.contains("Gecko/");
				if (!gecko) {
					presto = userAgent.contains("Presto/");
				}
			}
		}
	}
	  /**
	  *  说明:客户端是ie浏览器
	  * @param httpRequest 请求
	  * @return boolean 
	  * @update 2012-9-21 下午5:09:16
	   */
	public boolean isTrident() {
		return trident;
	}
   /**
   * 说明:客户端是否是webkit浏览器引擎
   * @param httpRequest 请求
   * @return boolean 
   * @update 2012-9-21 下午5:07:19
    */
	public boolean isWebKit() {
		return webKit;
	}
	  /**
	   * 说明:客户端是否是Gecko浏览器引擎
	   * @param httpRequest 请求
	   * @return boolean
	   * @update 2012-9-21 下午5:08:35
	    */
	public boolean isGecko() {
		return gecko;
	}
	/**
	 * 
	* 说明：是否为 Opera浏览器
	* @param httpRequest
	* @return boolean
	* @update 2012-10-28 下午5:16:25
	 */
	public boolean isPresto() {
		return presto;
	}
   /**
    * 
   * 说明：获得版本
   * @return int
   * @update 2012-11-5 下午3:09:00
    */
	public int getVersion() {
		return version;
	}

	private static final long serialVersionUID = 1798765474943067839L;
}
